﻿using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Navigation;

namespace FabioFranzini.SP2010.AppModel.SPHosted.AppFramework
{
    internal class AppNavigation
    {
        public static void AddAppLinkToQuickLaunch(SPWeb web)
        {
            Uri webUri = new Uri(web.Url);
            Uri appUri = new Uri(webUri, AppManifest.StartPage.Replace("~appWebUrl/", webUri.AbsoluteUri));

            SPNavigationNode appNode = web.Navigation.GetNodeByUrl(appUri.AbsoluteUri);

            if (appNode == null)
            {
                appNode = new SPNavigationNode(AppManifest.Title, appUri.AbsolutePath);
                web.Navigation.QuickLaunch.AddAsLast(appNode);
            }
        }

        public static void RemoveAppLinkToQuickLaunch(SPWeb web)
        {
            Uri webUri = new Uri(web.Url);
            Uri appUri = new Uri(webUri, AppManifest.StartPage.Replace("~appWebUrl/", webUri.AbsoluteUri));

            SPNavigationNode appNode = web.Navigation.GetNodeByUrl(appUri.AbsoluteUri);

            if (appNode != null)
                appNode.Delete();
        }
    }
}
